define(['app','pagination','datetime','editor'], function( appCRM ) {
  'use strict';
  /*后台日志操作管理*/
  appCRM.controller('Log', function($scope, dataFactoryCRM, $q) {
    $scope.logtypes = [{k: '0',v: '日报',cs:''}, {k: '1',v: '周报',cs:''}, {k: '2',v: '月报',cs:''}];/*查看页面日志分类*/
    $scope.logtypes2 = [{k: '0',v: '全部',cs:''},{k: '1',v: '日报',cs:''}, {k: '2',v: '周报',cs:''}, {k: '3',v: '月报',cs:''}];/*日志类型信息　*/
    $scope.logs;
    $scope.s_logtype;
    $scope.s_logtype2;
    $scope.s_title = '';
    $scope.s_content = '';
    $scope.logspage = {pageAll: 1,pageNow: 1, pageSize:12,k: 3};
    $scope.logspage2 = {pageAll: 1,pageNow: 1, pageSize:12,k: 3};
    $scope.richtxtAdd = '';
    $scope.richtxtEditLog;
    $scope.values;
    /*系统错误调用*/
    $scope.msgAttention = function() {
        $('.ui.systemerror').modal({
            closable: false,
            onDeny: function() {
                window.alert('Wait not yet!');
                return false;
            },
            onApprove: function() {
                $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
            }
        }).modal('show');

        setTimeout(function() {
            var system = $('.ui.dimmer.modals.page.transition.visible.active');
            if (system) {
                $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
            }
        }, 1000);
    };
    $scope.selLogtype = function( _type ){
          for(var i=0;i<$scope.logtypes2.length;i++){
              if( _type != i )
                  $scope.logtypes2[i]['cs'] = '';
              else{
                  if(  $scope.logtypes2[i]['cs'] == 'active' ){
                      $scope.s_logtype2 = -1;
                      $scope.logtypes2[i]['cs'] = '';
                  }
                  else{
                      $scope.logtypes2[i]['cs'] = 'active';
                      $scope.s_logtype2 = _type-1;
                  }
              }
          }
          $scope.logDataList();
    }

    $scope.logDataList = function(type) {
      if(type == -1) $scope.logtypes2[0]['cs'] = 'active';
      if(type) $scope.s_logtype2 = type;
      var _url = 'log/index?p=' + $scope.logspage.pageNow + '&psize=' + $scope.logspage.pageSize;
      _url = _url + '&type='+$scope.s_logtype2+'&title='+encodeURIComponent( $scope.s_title);
      _url = _url + '&content=' + encodeURIComponent( $scope.s_content);
      $scope.$emit("BUSY");
      dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
        $scope.rcode = _d.rcode;
        $scope.logspage.pageSize = _d.psize;
        $scope.logspage.pageAll = _d.pall;
        $scope.logspage.pageNow = _d.p;
        $scope.logs = _d.data;
      });
      $scope.$emit("NOTBUSY");
    }

    $scope.search = function() {
      $scope.logDataList();
    }

    /*默认加载“全部”日志*/
    $scope.logDataList(-1);

    /*日志添加界面*/
    $scope.showAdd = function() {
      $('#frm-add input').val('');
      $('#frm-add textarea').val('');
      $('.ui.selection.dropdown>.text').text('请选择日志类型');
      $scope.richtxtAdd = '';
      $('.ui.add').modal({
        onApprove: function() {
          if( $('#frm-add').form('is valid') ){
              $scope.addLog($('#frm-add').serialize()+'&content='+$scope.richtxtAdd );
          }
          else
              return false;
        }
      }).modal('show');
    }

    /*日志添加功能*/
    $scope.addLog = function(_d) {
      $scope.$emit("BUSY");
      $scope.busy = dataFactoryCRM.postDataByAPI('log/add', _d)
        .then(function(d) {
          $scope.logRcode = d.rcode;
          $scope.logMsg = d.msg;
          if (d.rcode) {
            d.data.typename = '';
            for (var st in $scope.logtypes) {
              if (parseInt($scope.logtypes[st].k) == d.data.type) {
                d.data.typename = $scope.logtypes[st].v;
                break;
              }
            }
            $scope.logDataList($scope.s_logtype2);
          } else {
            $scope.msgAttention();
            $scope.logDataList($scope.s_logtype2);
          }
        });
    }

    /*编辑日志信息*/
    $scope.showEdit = function() {
      var _vlog = this.vlog;
      $('#edit-sel').dropdown('set selected', _vlog.type);
      $scope.edit_title = _vlog.title ;
      $scope.edit_log_id = _vlog.log_id;
      $scope.richtxtEditLog = _vlog.content ;
      $scope.edit_create_time = _vlog.create_time ;
      $scope.edit_update_time = _vlog.update_time ;
      $('.ui.edit').modal({
        onApprove: function() {
          if( !$('#frm-edit').form('is valid') ){
              return false;
          }
          $scope.$emit("BUSY");
          dataFactoryCRM.postDataByAPI('log/edit', $('#frm-edit').serialize()+'&contents='+$scope.richtxtEditLog+'&log_id='+_vlog.log_id )
            .then(function(d) {
              if (d.rcode) {
                if ($scope.logs) {
                  var i = 0;
                  d.data.typename = '';
                  for (var st in $scope.logtypes) {
                    if (parseInt($scope.logtypes[st].k) == d.data.type) {
                      d.data.typename = $scope.logtypes[st].v;
                      break;
                    }
                  }
                  for (; i < $scope.logs.length; i++) {
                    if ($scope.logs[i].log_id == _vlog.log_id) {
                      $scope.logs[i] = d.data;
                      break;
                    }
                  }
                }
                $scope.logDataList($scope.s_logtype2);
                return true;
              } else {
                $scope.logDataList($scope.s_logtype2);
                return false;
              }

            });
          return true;
        }
      }).modal('show');
    }

    /*删除日志*/
    $scope.delLog = function() {
      var _vlog = this.vlog;
      $('#del-model .content p').html("确认删除 【" + _vlog.typename + "】" + _vlog.title);
      $('.ui.small.del').modal({
        onDeny: function() {
          $scope.$emit("BUSY");
          dataFactoryCRM.getDataByAPI('log/remove?id=' + _vlog.log_id)
            .then(function(d) {
              $scope.logRcode = d.rcode;
              $scope.logMsg = d.msg;
              if (d.rcode) {
                if ($scope.logs) {
                  var i = 0;
                  for (; i < $scope.logs.length; i++) {
                    if ($scope.logs[i].log_id == _vlog.log_id) {
                      $scope.logs.splice(i, 1);
                      break;
                    }
                  }
                }
                $scope.$emit("NOTBUSY");
                return true;
              } else {
                $scope.$emit("NOTBUSY");
                $scope.msgAttention();
                return false;
              }
              $scope.$emit("NOTBUSY");
            });
          return true;
        }
      }).modal('show');
    }

    $scope.user_ids = '';    //我的日志审核人的user_id
    $scope.nickname = '';   //我的日志审核人昵称
    $scope.getaudit = function(user_ids){
        if(user_ids) $scope.user_ids = user_ids;
        dataFactoryCRM.getDataByAPI('log/getaudit?id='+$scope.user_ids).then(function(_d) {
        if (_d.rcode=="1") {
          $scope.nickname = _d.data.nickname;
        } else {
          return false;
        }
      });
    }

    $scope.logstate = '';          //日志审核状态
    $scope.log_nickname = '';      //当前用户已审核日志的审核人用户名
    /*查看自己日志审核情况*/
    $scope.showLogs = function(){
      var _vlog = this.vlog;
      var typeName = "";
      switch (_vlog.type) {
        case '0': 
          typeName = '日报';
          break;
        case '1': 
          typeName = '周报';
          break;
        case '2': 
          typeName = '月报';
          break;
      }
      $('#log_title').text(_vlog.title);
      $('#log_type_name').text(typeName);
      $scope.view_log_id = _vlog.log_id;
      $scope.logstate = _vlog.audit_res;
      document.getElementById("log_contents").innerHTML = _vlog.content;
      $('#log_audit_msglog').text( _vlog.audit_msg);
      $('#log_audit_time').text(_vlog.audit_time);
      $scope.log_nickname = $scope.getaudit(_vlog.audit_id);
      $('#log_nickname').text($scope.log_nickname);
      $('.ui.shows.viewedlogs').modal('show');
    }

    /*查看我的日志信息*/
    $scope.showmylog = function(){
      var _vlog = this.vlog;
      var typeName = "";
      switch (_vlog.type) {
        case '0': 
          typeName = '日报';
          break;
        case '1': 
          typeName = '周报';
          break;
        case '2': 
          typeName = '月报';
          break;
      }
      $('#mylog_title').text(_vlog.title);
      $('#mylog_type_name').text(typeName);
      $scope.show_log_id = _vlog.log_id
      document.getElementById("show_mylogs").innerHTML = _vlog.content;
      $("#mylog_create_time").text(_vlog.create_time);
      $("#mylog_update_time").text(_vlog.update_time);
      $('#audit_msglogs').text( _vlog.audit_msg);
      $('#audit_times').text(_vlog.audit_time);
      $scope.log_nickname = $scope.getaudit(_vlog.audit_id);
      $scope.audit_res = _vlog.audit_res;
      $('.ui.my.logs.show').modal('show');
    }

    $scope.r_logtypes = [{k: '0',v: '全部',cs:''},{k: '1',v: '日报',cs:''}, {k: '2',v: '周报',cs:''}, {k: '3',v: '月报',cs:''}]; /*日志类型信息　*/
    $scope.r_logs;
    $scope.rs_logtype;
    $scope.rs_title = '';
    $scope.rs_nickname = '';
    $scope.key;
    $scope.rs_search = function() {
      $scope.reviewLogList();
    }

    /*按照日志类型查看*/
    $scope.rewLogtype = function( _type ){
          for(var i=0;i<$scope.r_logtypes.length;i++){
              if( _type != i )
                  $scope.r_logtypes[i]['cs'] = '';
              else{
                  if(  $scope.r_logtypes[i]['cs'] == 'active'){
                      $scope.rs_logtype = -1;
                      $scope.r_logtypes[i]['cs'] = ''
                  }
                  else{
                      $scope.r_logtypes[i]['cs'] = 'active';
                      $scope.rs_logtype = _type-1;
                  }
              }
          }
          $scope.reviewLogList();
    }

    /*当前用户可以审核日志列表*/
    $scope.reviewLogList = function(key,type){
      if(key) $scope.key = key;
      if(type) $scope.rs_logtype = type;
        if($scope.key =="1"){
            $("#demo1").addClass("blue");$("#demo2").removeClass("blue");$("#demo3").removeClass("blue");
        }else if($scope.key =="2"){
            $("#demo1").removeClass("blue");$("#demo2").addClass("blue");$("#demo3").removeClass("blue");
        }else if($scope.key == "3"){
            $("#demo2").removeClass("blue");$("#demo1").removeClass("blue");$("#demo3").addClass("blue");
        }
      if(type == -1) $scope.r_logtypes[0]['cs'] = 'active';
      var _url = 'log/reviewlist?p=' + $scope.logspage2.pageNow + '&psize=' + $scope.logspage2.pageSize;
      _url = _url + '&state='+$scope.key+'&type='+$scope.rs_logtype+'&title='+encodeURIComponent( $scope.rs_title);
      _url = _url + '&nickname=' + encodeURIComponent( $scope.rs_nickname);
      $scope.$emit("BUSY");
      dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
        if (_d.rcode=="1") {
          $("#w_log_list").hide();
          $("#y_log_list").show();
          $scope.logspage2.pageSize = _d.psize;
          $scope.logspage2.pageAll = _d.pall;
          $scope.logspage2.pageNow = _d.p;
          $scope.r_logs = _d.data;
        } else if(_d.rcode=="2"){
          $("#y_log_list").hide();
          $("#w_log_list").show();
          $("#logspages_shows").hide();
        }
        $scope.$emit("NOTBUSY");
      });
    }
    $scope.login_info;
    $scope.reviewLogList(1,-1);

    /*审核日志*/
    $scope.reviewLog = function(){
      $('#add_sty').removeClass('ui basic red pointing prompt label transition visible');
      document.getElementById("add_sty").innerHTML ='';
      var _vlog = this.vlogs;
      var type_namess = "";
      switch (_vlog.type) {
        case 0: 
          type_namess = '日报';
          break;
        case 1: 
          type_namess = '周报';
          break;
        case 2: 
          type_namess = '月报';
          break;
      }
      $('#audit_title').text(_vlog.title);
      $('#audit_typename').text(type_namess);
      $scope.review_log_id = _vlog.log_id;
      document.getElementById("audit_content").innerHTML = _vlog.content;
      $scope.audit_msgs = _vlog.audit_msg;
      $scope.auditMsgLog = "";
      $("#ver_infoss").removeClass("field required");
      $scope.audit_msg = _vlog.audit_msg;
      $scope.audit_res = _vlog.audit_res;
      $('.ui.review').modal('show');
    }

    /*日志审核通过*/
    $scope.veriok = function(){
      $scope.$emit("BUSY");
      $("#ver_infoss").removeClass("field required");
      $("#ver_infoss").removeClass("ui basic red pointing prompt label transition visible");
      dataFactoryCRM.getDataByAPI('log/reviewok?log_id='+$scope.review_log_id+'&audit_msg='+$scope.auditMsgLog,
        $('#frm-review').serialize()).then(function(_d) {
        $scope.logRcode = _d.rcode;
        $scope.logMsg = _d.msg;
        if (_d.rcode) {
          $scope.logspage.pageSize = _d.psize;
          $scope.logspage.pageAll = _d.pall;
          $scope.logspage.pageNow = _d.p;
          $scope.r_logs = _d.data;
        } else {
          $scope.msgAttention();
          return false;
        }
        $('.ui.review').modal('hide');
        $scope.logDataList();
        $scope.reviewLogList();
      });
    }

    /*日志审核不通过功能*/
    $scope.verino = function(){
      $scope.$emit("BUSY");
      $("#ver_infoss").addClass("field required");
        if( $scope.auditMsgLog ){
          $('#add_sty').removeClass('ui basic red pointing prompt label transition visible');
          document.getElementById("add_sty").innerHTML ='';
          dataFactoryCRM.getDataByAPI('log/reviewno?log_id='+$scope.review_log_id+'&audit_msg='+$scope.auditMsgLog,
            $('#frm-review').serialize()).then(function(_d) {
              $scope.logRcode = _d.rcode;
              $scope.logMsg = _d.msg;
            if (_d.rcode) {
              $scope.logspage.pageSize = _d.psize;
              $scope.logspage.pageAll = _d.pall;
              $scope.logspage.pageNow = _d.p;
              $scope.r_logs = _d.data;
            } else {
              $scope.msgAttention();
              $scope.$emit("NOTBUSY");
              return false;
            }
            $('.ui.review').modal('hide');
            $scope.logDataList();
            $scope.reviewLogList();
          });
        }else{
          $('#add_sty').addClass('ui basic red pointing prompt label transition visible');
          document.getElementById("add_sty").innerHTML ='请填写审核说明！';
          return false;
        }
    }

    $scope.audit_nicknamess = '';   //审核人昵称
    /*查看审核信息*/
    $scope.showReview = function(){
        var _vlog = this.vlogs;
        var show_typeName = "";
        switch (_vlog.type) {
          case 0: 
            show_typeName = '日报';
            break;
          case 1: 
            show_typeName = '周报';
            break;
          case 2: 
            show_typeName = '月报';
            break;
        }
        $('#view_title').text(_vlog.title);
        $('#view_type_name').text(show_typeName);
        $scope.showview_log_id = _vlog.log_id;
        document.getElementById("viewLogContentss").innerHTML = _vlog.content;
        $('#view_audit_msglog').text( _vlog.audit_msg);
        $('#view_audit_time').text(_vlog.audit_time);
        if(_vlog.audit_id){                 //已审核，即存在审核人id则显示昵称
            dataFactoryCRM.getDataByAPI('log/getUname?user_id='+_vlog.audit_id).then(function(_d) {
                $scope.audit_nicknamess = _d.nickname;
                $('#view_nickname').text($scope.audit_nicknamess);
           });  
        }else{
            $('#view_nickname').text('');//未审核日志，不显示审核人名字
        }
        $('.ui.longs.modal').modal('show');
    }

  });
});